<template>
  <div class="main-table"
  :class="'main-table--' + layoutSkin">
    <el-table
        ref="grid"
        v-loading="loading"
        :height="height"
        v-on="$listeners"
        v-bind="$attrs"
        style="width: 100%">
      <el-table-column
        v-if="showSelection"
        v-bind="selectionColumns"
        type="selection">
      </el-table-column>
      <el-table-column
        v-if="showIndex"
        width="50"
        label="序号"
        fixed="left"
        align="center"
        type="index">
      </el-table-column>
      <!-- 数据栏 -->
      <template  v-for="(item,index) in _columns">
        <el-table-column
          :align="item.align || 'center'"
          :key="index" v-bind="item" v-if="$scopedSlots[item.prop]" >
          <template slot-scope="scope">
            <slot :name="item.prop" v-bind="scope"></slot>
          </template>
        </el-table-column>
        <el-table-column
          v-else
          :align="item.align || 'center'"
          :prop="item.prop"
          :key="index" v-bind="item">
          <template v-if="item.children && item.children.length" >
            <main-column :item="item"></main-column>
          </template>
        </el-table-column>
      </template>
      <!-- 操作栏 -->
      <el-table-column
        v-if="$scopedSlots.handle"
        :width="handleColumns.width || '150px'"
        v-bind="handleColumns">
        <template slot-scope="scope">
          <slot name="handle" v-bind="scope"></slot>
        </template>
      </el-table-column>
      <template slot="empty" >
        <div class="empty-data">
          <img src="/static/images/base/table-emptydata01.png" v-if="layoutSkin=== 'dark'" />
          <img src="/static/images/base/table-emptydata02.png" v-else />
        </div>
      </template>
    </el-table>
    <el-pagination
      v-if="pageTotal"
      background
      v-on="$listeners"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageTotal">
    </el-pagination>
  </div>
</template>
<script>
import methods from './methods';
import MainColumn from './column';
export default {
  name: 'MainTable',
  components: {
    MainColumn

  },
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    /** 对应列的类型。如果设置了 selection 则显示多选框；
   * 如果设置了 index 则显示该行的索引（从 1 开始计算）；
   * 如果设置了 expand 则显示为一个可展开的按钮 */
    columnType: {
      type: String | Array,
      default: () => []
    },
    selectionColumns: {
      type: Object,
      default: () => {
        return {
          fixed: 'left',
          align: 'right'
        };
      }
    },
    columns: {
      type: Array,
      default: () => []
    },
    handleColumns: Object,
    currentPage: Number,
    pageTotal: Number,
    // 每页条数
    pageSize: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default: () => [20, 30, 40, 50]
    }

  },
  mixins: [methods],
  computed: {
    _columns() {
      return this.columns.reduce((item, next) => {
        if (next.formatter === undefined) {
          next.formatter = (row, column, cellValue, index) => {
            return this.isEmpty(cellValue);
          };
        }
        item.push(next);
        return item;
      }, []);
    },
    showSelection() {
      return this.columnType.includes('selection');
    },
    showIndex() {
      return this.columnType.includes('index');
    },
    layoutSkin: {
      get() { return this.$store.state.common.layoutSkin; }
    }
  },
  data() {
    return {
      height: '100%',
      dataEmpty: ''
    };
  },
  methods: {
    isEmpty(value) {
      if (value === '' || value === null || value === undefined) {
        return '--';
      } else {
        return value;
      }
    }
  },
  mounted() {
    // console.log(`attrs`, this.$attrs);
  },
  created() {
    const documentClientHeight = this.$store.state.common.documentClientHeight;

    // main-content.vue 70行
    var siteContentViewHeight = documentClientHeight - 70 - 30 - 2 - 40;
    var tableHeight = siteContentViewHeight - 40 - 51; // 减去ert  减去头部form
    if (this.pageTotal !== undefined) { // 有分页
      tableHeight = tableHeight - 47;
    }
    this.height = this.$attrs.height || tableHeight;
  },
  watch: {
    '$attrs.data'(n) {
      this.$nextTick(_ => {
        this.$refs.grid.doLayout();
      });
    }
  }

};
</script>

<style lang="scss" scoped>
 .empty-data{
    img{
      width: 120px;
    }
 }
.el-pagination {
  margin-top: 15px;
  text-align: center;
}
</style>
